extends Control

@onready var roomIdLabel = $RoomInfo/RoomId
@onready var roomDescLabel = $RoomInfo/RoomDesc
@onready var userContainer = $Members/VBoxContainer
@onready var notMasterOfRoom:Control = $NotMasterOfRoom
@onready var masterOfRoom:Control = $MasterOfRoom
@onready var notMasterLeaveButton = $NotMasterOfRoom/Leave
@onready var MasterLeaveButton = $MasterOfRoom/Leave
@onready var cancelReadyButton = $NotMasterOfRoom/CancelReady
@onready var readyButton = $NotMasterOfRoom/Ready
@onready var gamingMask = $GamingMask

var room

func _ready():
	GlobalSignals.NM_LEAVEROOM_RESULT.connect(NM_LEAVEROOM_RESULT_handler)
	GlobalSignals.NM_GET_ROOMINFO_RESULT.connect(NM_GET_ROOMINFO_RESULT_handler)
	GlobalSignals.NM_JOINROOM_RESULT.connect(NM_JOINROOM_RESULT_handler)
	GlobalSignals.NM_ROOM_CHANGED.connect(NM_ROOM_CHANGED_handler)
	GlobalSignals.NM_GAME_START.connect(NM_GAME_START_handler)
	GlobalSignals.NM_GAME_END.connect(NM_GAME_END_handler)
	
	getRoomInfo()
	
	
func _exit_tree():
	GlobalSignals.NM_GAME_END.disconnect(NM_GAME_END_handler)
	GlobalSignals.NM_LEAVEROOM_RESULT.disconnect(NM_LEAVEROOM_RESULT_handler)
	GlobalSignals.NM_GET_ROOMINFO_RESULT.disconnect(NM_GET_ROOMINFO_RESULT_handler)
	GlobalSignals.NM_JOINROOM_RESULT.disconnect(NM_JOINROOM_RESULT_handler)	
	GlobalSignals.NM_ROOM_CHANGED.disconnect(NM_ROOM_CHANGED_handler)
	GlobalSignals.NM_GAME_START.disconnect(NM_GAME_START_handler)
	
	
func initBottomButton():
	var targetUser
	for user in room["users"]:
		if user["userId"] == Global.currentUser["userId"]:
			targetUser = user
	
	if targetUser["isRoomMaster"]:
		notMasterOfRoom.visible = false
		masterOfRoom.visible = true
	else:
		notMasterOfRoom.visible = true
		masterOfRoom.visible = false
		
		if targetUser["ready"]:
			cancelReadyButton.visible = true
			readyButton.visible = false
		else:
			cancelReadyButton.visible = false
			readyButton.visible = true
		

func initUserList():
	clearUserList()
	
	for user in room["users"]:
		var roomMemberNode:RoomMemberItem = Global.createSceneInst("RoomMemberItem") as RoomMemberItem
		userContainer.add_child(roomMemberNode)
		roomMemberNode.init(user)
		
		
func canStartGame():
	var result = ""
	
	if room["users"].size() <= 1:
		result = "请等待玩家加入"
		return result
	
	for user in room["users"]:
		if not user["isRoomMaster"] and user["ready"] == false:
			result = "请等待玩家准备"
			return result
	
	return result


func clearUserList():
	for child in userContainer.get_children():
		child.queue_free()


func _on_leave_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	
	var data = {
		"roomId": room["roomId"]
	}
	
	NetClient.sendMessage(NetMessages.NM_LEAVEROOM, data)
	
	
func NM_LEAVEROOM_RESULT_handler(data):
	var leaveUser = data["user"]
	var room = data["room"]
	if leaveUser["userId"] == Global.currentUser["userId"]:
		Global.changeScene("RoomList")
	else:
		getRoomInfo()
		

func NM_GAME_END_handler(data):
	getRoomInfo()
		
		
func NM_JOINROOM_RESULT_handler(data):
	getRoomInfo()
	

func NM_ROOM_CHANGED_handler(data):
	getRoomInfo()
	
	
func NM_GAME_START_handler(data):
	Global.changeScene("MainGameMultiplayer")
	Global.changeScrennOrientation2Landscape()
		

func getRoomInfo():
	var data = {}
	NetClient.sendMessage(NetMessages.NM_GET_ROOMINFO, data)
	

func updateRoomDisplay():
	room = Global.currentRoom
	roomIdLabel.text = str(room["roomId"])
	roomDescLabel.text = room["roomDesc"]
	gamingMask.visible = room["state"] == 1
	
	initBottomButton()
	initUserList()
	
	
func NM_GET_ROOMINFO_RESULT_handler(data):
	Global.currentRoom = data["room"]
	room = Global.currentRoom
	
	updateRoomDisplay()


func _on_start_game_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	
	var result = canStartGame()
	if result != "":
		Toast.showMessage(result)
		return
	
	var data = {}
	NetClient.sendMessage(NetMessages.NM_START_GAME, data)


func _on_ready_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	var data = {}
	NetClient.sendMessage(NetMessages.NM_READY, data)


func _on_cancel_ready_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	var data = {}
	NetClient.sendMessage(NetMessages.NM_CANCEL_READY, data)
	

func _on_gamingmask_return_button_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	_on_leave_pressed()
